<template>
	<view class="pathway on-pathway" @touchend="onEnd">
		<view class="tips">
			<text v-if="isOk" style="color: #FFFFFF;">正在运营</text>
			<text v-else class="di-text">{{time}} 开始行程</text>
		</view>
		<view class="track" :style="{'transform':'translateX('+oldx+'px)'}"></view>
		<movable-area :animation="true">
			<movable-view class="on-track" :class="{'active':isOk}" 
				:x="x" direction="horizontal" @change="onMove"
				:disabled="isOk || ((dateDpStr-aheadTime) > timestamp)"
			></movable-view>
		</movable-area>

	</view>
</template>

<script>
	export default {
		name: 'start_plan_move',
		props: {
			time: {
				type: String,
				default: ''
			},
			timestamp: {
				type: Number,
				default: 0,
			},
			dateDpStr: {
				type: Number,
				default: 0,
			},
			isRest:{
				type: Boolean,
				default: false
			},
			
		},
		watch: {
			isRest(nVal, oVal) {
				if(nVal==true){
					this.reset()
				}
			}
		},
		data() {
			return {
				x: 0,
				oldx:0,
				isOk: false,
				size: {},
				count:0,
				isMove:false,
				aheadTime: 600

			};
		},
		
		mounted() {
			let getSize = (selector) => {
				return new Promise((resolve, reject) => {
					let view = uni.createSelectorQuery().in(this).select(selector);
					view.fields({
						size: true,
					}, (res) => {
						resolve(res.width);
					}).exec();
				});
			}
			this.$nextTick(() => {
				getSize(".on-pathway").then((res1) => {
					this.size.pathway = res1;
					getSize(".on-track").then((res2) => {
						this.size.track = res2;
					});
				})
			})
		},
		methods: {
			/* 滑动中 */
			onMove(e) {
				/*还未到发车前10分钟**/
				// if((this.dateDpStr-600) < this.timestamp){
				// 	// this.$u.toast('请于'+this.time+'开始行程')
				// 	console.log(1111)
				// 	this.reset()
					
				// 	return 
				// }
				this.oldx = e.detail.x;
			},
			/* 滑动结束 */
			onEnd() {
				/*还未到发车前10分钟**/
				if((this.dateDpStr-this.aheadTime) > this.timestamp){
					this.$u.toast('请于'+this.time+'开始行程')
					this.reset()
					
					return 
				}
				if (this.isOk || this.oldx<1) {
					return;
				}
				
				this.count++;
				this.x = this.oldx;
				if ((this.oldx + 3) > (this.size.pathway - this.size.track)) {
					this.isOk = true;
					this.$emit("result",{flag:true,count:this.count});
				} else {
					this.$nextTick(() => {
						setTimeout(() => {
							this.x = 0;
							this.oldx = 0;
						}, 100);
					});
					this.$emit("result",{flag:false,count:this.count});
				}
			},
			/* 重置 */
			reset(){
				this.$nextTick(function() {
						this.x = 0;
						this.oldx = 0;
						this.count = 0;
						this.isOk = false;
						this.$emit("isRest");
					})
				
			}
		}
	}
</script>

<style scoped lang="scss">
	.di-text{
		color: #FFFFFF;
		font-size: 20px;
		font-weight: 700;
	}
	.pathway {
		height: 130rpx;
		width: 100%;
		background-color: #7ac23c;
		position: relative;
		overflow: hidden;
		
		/* 提示信息 */
		.tips {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			line-height: 130rpx;
			text-align: center;
			color: #666;
			font-size: 32rpx;
			z-index: 3;
		}
		
		/* 滑动轨道 */
		.track {
			position: absolute;
			top: 0;
			left: 0;
			background-color: #74beff;
			width: 100%;
			height: 100%;
			padding-left: 0;
			box-sizing: content-box;
			transform: translateX(0);
		}
		
		movable-area {
			position: absolute;
			top: 0;
			left: 0;
			height: 100%;
			width: 100%;
			background: none;
			z-index: 5;
		}
		
		movable-view {
			height: 100%;
			width: 160rpx;
			box-sizing: border-box;
			background-color: #fff;
			border: #dddddd solid 1px;
			background-position: center;
			background-repeat: no-repeat;
			background-size: auto 32rpx;
			background-image: url("");
		}
		
		movable-view.active {
			border: #7ac23c solid 1px;
			background-image: url("");
		}
		
		/* 禁止 */
		.disabled {
			position: absolute;
			z-index: 10;
			height: 100%;
			width: 100%;
			top: 0;
			left: 0;
		}
	}
</style>